package com.song.batchoperationrv;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.ActionMode;
import android.view.MenuInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.song.batchoperationrv.adapter.RvItemAdapter;
import com.song.batchoperationrv.callback.OnItemListener;
import com.song.batchoperationrv.entity.ItemModel;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.rv_list)
    RecyclerView mRvList;
    @BindView(R.id.lly_menu)
    LinearLayout llyMenu;
    @BindView(R.id.tv_delete)
    TextView tvDelete;
    @BindView(R.id.tv_select_all)
    TextView tvSelectAll;
    List<ItemModel> mDataList;
    //记录Menu的状态
    private boolean isShow;
    private boolean isSelectAll;
    RvItemAdapter mRvItemAdapter;
    //记录选择的Item
    private HashSet<Integer> positionSet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        ButterKnife.bind(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        initView();
        initData();
        setListener();
    }

    private void initView() {
        mRvList.setLayoutManager(new LinearLayoutManager(this));
    }

    private void initData() {

        positionSet =  new HashSet<>();
        mDataList = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            ItemModel item = new ItemModel();
            item.content = i + "";
            mDataList.add(item);
        }
        mRvItemAdapter = new RvItemAdapter(this, mDataList);
        mRvList.setAdapter(mRvItemAdapter);
    }

    private void setListener() {

        mRvItemAdapter.setOnItemListener(new OnItemListener() {

            @Override
            public void checkBoxClick(int position) {
                //已经有Item被选择,执行添加或删除操作
                addOrRemove(position);
            }

            @Override
            public void onItemClick(View view, int position) {
                //触发Item的单击事件
                Toast.makeText(MainActivity.this,String.format(getString(R.string.hint),position),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {
                if (!isShow) {
                    isShow = true;
                    llyMenu.animate()
                            .alpha(1.0f)
                            .translationYBy(-llyMenu.getHeight())
                            .setDuration(500).start();
                    ((CheckBox) view.findViewById(R.id.cb_select)).setChecked(true);
                    mDataList.get(position).isSelect = true;
                    positionSet.add(position);
                }
            }
        });
    }

    /**
     * 操作Item记录集合
     */
    private void addOrRemove(int position) {
        if(positionSet.contains(position)) {
            // 如果包含，则撤销选择
            Log.e("----","remove");
            positionSet.remove(position);
        } else {
            // 如果不包含，则添加
            Log.e("----","add");
            positionSet.add(position);
        }

        if(positionSet.size() == 0) {
            isShow = false;
            llyMenu.animate()
                    .alpha(0.0f)
                    .translationYBy(llyMenu.getHeight())
                    .setDuration(500).start();
        }
    }


    /**
     * 删除
     */
    @OnClick(R.id.tv_delete)
    void delete() {
        HashSet<ItemModel> valueSet = new HashSet<>();
        for (Integer integer : positionSet) {
            valueSet.add(mRvItemAdapter.getItem(integer));
        }
        for (ItemModel itemModel : valueSet) {
            mRvItemAdapter.remove(itemModel);
        }
        mRvItemAdapter.notifyDataSetChanged();
        positionSet.clear();
    }

    /**
     * 全选
     */
    @OnClick(R.id.tv_select_all)
    void selectAll() {
        if(!isSelectAll) {
            isSelectAll = true;
            for (int i = 0; i < mDataList.size(); i++) {
                mDataList.get(i).isSelect = true;
                positionSet.add(i);
            }
            mRvItemAdapter.notifyDataSetChanged();
        } else {
            isSelectAll = false;
            for (int i = 0; i < mDataList.size(); i++) {
                mDataList.get(i).isSelect = false;
                positionSet.remove(i);
            }
            mRvItemAdapter.notifyDataSetChanged();
        }
    }
}
